﻿namespace Storm.Docking.Visual.Glyphs
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Text;
    using System.Windows.Forms;

	using Storm;
	using Storm.Docking;
	using Storm.Docking.Controls;
	using Storm.Docking.Controls.Float;
	using Storm.Docking.Visual;
    using Storm.Docking.Visual.Drawing;

    /// <summary>
    /// Represents the options button of a DockCaption.
    /// </summary>
    [ToolboxItem(false)]
    public class OptionsGlyph
        : Glyph
    {
        #region Fields

        private ContextMenu contextMenu  = null;
        private MenuItem    dockedItem   = null;
        private MenuItem    floatingItem = null;

        #endregion

        #region Methods

        #region Internal

		internal override void OnClickAction()
		{
			floatingItem.Checked = this.DockCaption.DockPanel.FindForm() is DockForm == true;
			dockedItem.Checked = this.DockCaption.DockPanel.FindForm() is DockForm == false;

			contextMenu.Show(this.DockCaption.DockPanel.DockPane,
				new Point(Cursor.Position.X, (Cursor.Position.Y -
					Cursor.Size.Height) + this.Height + 2));
		}

        internal override void OnParentRecieveFocus(object sender, EventArgs e)
        {
			if (this.DockCaption.Focused == true)
			{
				this.Image.Image = this.DockCaption.Renderer.
					DockColorTable.OptionsGlyphFocusImage;
			}
			else
			{
				this.Image.Image = this.DockCaption.Renderer.
					DockColorTable.OptionsGlyphNormalImage;
			}
        }

        internal override void OnParentReleaseFocus(object sender, EventArgs e)
        {
            if (this.DockCaption.Focused == true)
				this.Image.Image = this.DockCaption.Renderer.
					DockColorTable.OptionsGlyphFocusImage;
            else
            {
				this.Image.Image = this.DockCaption.Renderer.
					DockColorTable.OptionsGlyphNormalImage;
            }
        }

        #endregion

        #region Private

        private void OnDockedClick(object sender, EventArgs e)
        {
			if (dockedItem.Checked == false)
			{
				this.DockCaption.DockPanel.MakeDocked();

				// Check whether the DockPanel actually got docked.
				if (this.DockCaption.DockPanel.DockPanelState == DockPanelState.Docked &&
					this.DockCaption.DockPanel.FindForm() is DockForm == false)
				{
					floatingItem.Checked = false;
					dockedItem.Checked = true;
				}
			}
        }

        private void OnFloatingClick(object sender, EventArgs e)
        {
			if (floatingItem.Checked == false)
			{
				dockedItem.Checked = false;
				floatingItem.Checked = true;

				this.DockCaption.DockPanel.MakeFloating();
			}
        }

        #endregion

        #endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="OptionsGlyph"/> class.
		/// </summary>
		/// <param name="parent">The parent.</param>
        public OptionsGlyph(DockCaption parent)
            : base(parent)
        {
            // Initialize our ContextMenu.
            contextMenu = new ContextMenu();

            dockedItem = new MenuItem();
            dockedItem.Text = "Docked";
            dockedItem.Checked = true;
            dockedItem.Click += OnDockedClick;

            floatingItem = new MenuItem();
            floatingItem.Text = "Floating";
            floatingItem.Checked = false;
            floatingItem.Click += OnFloatingClick;

            contextMenu.MenuItems.Add(dockedItem);
            contextMenu.MenuItems.Add(floatingItem);

			this.Image.Image = this.DockCaption.Renderer.
			    DockColorTable.OptionsGlyphNormalImage;
        }
    }
}
